/**
 * Created by rockyren on 14/12/2.
 */

describe('Unit: 购物车控制器',function(){
  beforeEach(module('cartApp'));
  var $httpBackend,$controller,$rootScope;
  beforeEach(inject(function(_$httpBackend_,_$controller_,_$rootScope_){
    $httpBackend = _$httpBackend_;
    $controller = _$controller_;
    $rootScope = _$rootScope_;
  }));

  describe('cartIndexController',function(){
    var cartIndexController,scope,
      baseCartUrl = 'api/cart',
      resCartList = [
        {
          id: 1,
          name: "book",
          image: "book.jpg",
          price: 123,
          count: 2,
          amount: 3
        },
        {
          id: 2,
          name: "bottle",
          image: 'bottle.jpg',
          price: 321,
          count: 4,
          amount: 5
        }
      ];

    beforeEach(function(){
      scope = $rootScope.$new();
      cartIndexController = $controller('cartIndexController',{$scope: scope});



      $httpBackend.expectGET(baseCartUrl).respond(200,resCartList);

      $httpBackend.flush();


    });

    it("加载完控制器后,cartList能加载为购物车列表",function(){
      expect(resCartList).toEqual(scope.cartList);
    });

    it('测试加载后的计数器,每次加减都会发出一次修改请求',function(){
      var countHandle1 = scope.Numbers[1];

      expect(countHandle1.getCount()).toEqual(2);

      /*测试plus部分*/
      $httpBackend.expectPUT(baseCartUrl + '/1',{count: 3}).respond(200);
      countHandle1.plus();
      $httpBackend.flush();

      expect(countHandle1.getCount()).toEqual(3);
      countHandle1.plus();
      expect(countHandle1.getCount()).toEqual(3);


      /*测试minus部分*/
      $httpBackend.expectPUT(baseCartUrl + '/1',{count: 2}).respond(200);
      countHandle1.minus();
      $httpBackend.flush();

      expect(countHandle1.getCount()).toEqual(2);
    });


    it('测试方法: delete',function(){
      $httpBackend.expectDELETE(baseCartUrl + '/1').respond(200);
      $httpBackend.expectGET(baseCartUrl).respond(200,resCartList);

      scope.delete(1);
      $httpBackend.flush();

    });


  });
});